package com.lession2.tcpFileConnection;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class Client {

    public static void main(String[] args) throws Exception {

        //客户端文件上传
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
        OutputStream os = socket.getOutputStream();
        //绝对路径
        FileInputStream fis = new FileInputStream(new File("D:\\IDEAProject\\mixed\\network\\src\\view.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while(-1 != (len=fis.read(buffer))){
            os.write(buffer,0,len);
        }

        //通知服务器我已经传输完了,关闭输出流
        socket.shutdownOutput();

        //确定服务器接收完毕后才能断开连接
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer2 = new byte[2014];
        int len2;
        while(-1 != (len2=is.read(buffer2))){
            baos.write(buffer2,0,len2);
        }
        System.out.println(baos.toString());

        fis.close();
        os.close();
        socket.close();


    }

}
